package com.bjqh.edu.UI.Activity.Login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Base.EasyActivity;
import com.bjqh.edu.Bean.Login;
import com.bjqh.edu.MainActivity;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Activity.Index.FreeReservationActivity;
import com.bjqh.edu.UI.Activity.TeacherPersonal.TeacherIndexActivity;
import com.bjqh.edu.UI.Utils.PersistentCookieStore;
import com.bjqh.edu.UI.Utils.SharedPrefsUtil;
import com.bjqh.edu.UI.dialog.LoadDialog;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class LoginActivity extends EasyActivity {

    @BindView(R.id.iv_logo)
    ImageView ivLogo;
    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.tv_pass_word_rt)
    TextView tvPassWordRt;
    @BindView(R.id.tv_rg)
    TextView tvRg;
    @BindView(R.id.iv_yj)
    ImageView ivYj;
    @BindView(R.id.sz)
    TextView sz;
    @BindView(R.id.checkbox)
    CheckBox checkbox;
    private boolean isHideFirst = true;// 输入框密码是否是隐藏的，默认为true

    private String userNameValue, passwordValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        etPhone.setText(SharedPrefsUtil.getValue(LoginActivity.this, "name", ""));
        etPwd.setText(SharedPrefsUtil.getValue(LoginActivity.this, "paw", ""));
        ivYj.setImageResource(R.mipmap.login_yj_pre);

        initViews();
        initData();
        initEvents();
    }


    @Override
    protected void initViews() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvents() {
        sz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("url", AppConstants.H5_URL + "login_agree.html");
                intent.setClass(LoginActivity.this, FreeReservationActivity.class);
                LoginActivity.this.startActivity(intent);
            }
        });
        ivYj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isHideFirst == true) {
                    ivYj.setImageResource(R.mipmap.login_yj);
                    //密文
                    HideReturnsTransformationMethod method1 = HideReturnsTransformationMethod.getInstance();
                    etPwd.setTransformationMethod(method1);
                    isHideFirst = false;
                } else {
                    ivYj.setImageResource(R.mipmap.login_yj_pre);
                    //密文
                    TransformationMethod method = PasswordTransformationMethod.getInstance();
                    etPwd.setTransformationMethod(method);
                    isHideFirst = true;

                }
                // 光标的位置
                int index = etPwd.getText().toString().length();
                etPwd.setSelection(index);
            }
        });
        tvPassWordRt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, PasswordSettingsActivity.class));

            }
        });
        btnLogin.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                userNameValue = etPhone.getText().toString();
                passwordValue = etPwd.getText().toString();
                if (TextUtils.isEmpty(etPhone.getText())) {

                    Toast.makeText(LoginActivity.this, "报告小主，账号还没有填呢~", Toast.LENGTH_SHORT).show();

                    etPhone.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(etPwd.getText())) {

                    Toast.makeText(LoginActivity.this, "报告小主，密码还没有填呢~", Toast.LENGTH_SHORT).show();
                    etPwd.requestFocus();
                    return;

                }
                if (!checkbox.isChecked()) {
                    Toast.makeText(getApplicationContext(), "报告小主，您还没有阅读守则条例哦~~", Toast.LENGTH_SHORT).show();
                    return;
                }
                LoadDialog.initDialog(LoginActivity.this, "正在登陆...");
                OkGo.getInstance().setCookieStore(PersistentCookieStore.instance());
                OkGo.post(AppConstants.BASE_URL + "/user/userLogin.shtml")
                        .tag(this)
                        .cacheKey("cacheKey")
                        .cacheMode(CacheMode.DEFAULT)
                        //   .headers("token",AppConstants.md5(etPhone.getText().toString()))
                        .params("passWord", AppConstants.md5(etPwd.getText().toString()))
                        .params("mobile", etPhone.getText().toString())
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                Gson gson = new Gson();
                                Log.e("登录", s);
                                Login login = gson.fromJson(s, Login.class);
                                if (login.getMsg().equals("success")) {
                                    // Toast.makeText(LoginActivity.this, "报告小主，登陆成功了哦~", Toast.LENGTH_SHORT).show();
                                   /* SharedPreferences.Editor editor = sp.edit();
                                    //保存用户名和密码
                                    editor.putString("uname", etPhone.getText().toString());
                                    editor.putString("upswd", etPwd.getText().toString());
                                    editor.putString("User", s);
                                    editor.commit();*/
                                    SharedPrefsUtil.putValue(LoginActivity.this, "name", etPhone.getText().toString());
                                    SharedPrefsUtil.putValue(LoginActivity.this, "paw", etPwd.getText().toString());
                                    SharedPrefsUtil.putValue(LoginActivity.this, "ltime", System.currentTimeMillis());
                                    SharedPrefsUtil.putValue(LoginActivity.this, "user", s);
                                    if (login.getRole().equals("0")) {
                                        // editor.putString("role", "0");
                                        SharedPrefsUtil.putValue(LoginActivity.this, "role", "0");
                                        Intent logoutIntent = new Intent(LoginActivity.this, MainActivity.class);
                                        logoutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(logoutIntent);
                                        LoadDialog.closeDialog();

                                    } else if (login.getRole().equals("1")) {
                                        //editor.putString("role", "1");
                                        SharedPrefsUtil.putValue(LoginActivity.this, "role", "1");
                                        Intent logoutIntent = new Intent(LoginActivity.this, TeacherIndexActivity.class);
                                        logoutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(logoutIntent);
                                        LoadDialog.closeDialog();
                                    }


                                } else {
                                    Toast.makeText(LoginActivity.this, "报告小主，您的手机号或密码输错了哦~~", Toast.LENGTH_SHORT).show();
                                    LoadDialog.closeDialog();
                                }


                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                Toast.makeText(LoginActivity.this, "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                                LoadDialog.closeDialog();
                            }
                        });


            }
        });
        tvRg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));


            }
        });

    }

}
